package de.hauschild.gmltracer.tracer.impl.tier1;

import org.apache.commons.math.geometry.Vector3D;

import de.hauschild.gmltracer.tracer.Intersection;
import de.hauschild.gmltracer.tracer.Ray;
import de.hauschild.gmltracer.tracer.SurfaceFunction;

/**
 * A {@link de.hauschild.gmltracer.tracer.Shape Shape} implementation representing a sphere with the radius 1.0 and
 * position at the coordinate systems origin.
 * 
 * @author Klaus Hauschild
 * @since 1.0.0
 */
public class Sphere extends AbstractBoundingSpheredShape {

  /**
   * Creates a new {@link Sphere} with the given surface function.
   * 
   * @param theSurfaceFunction
   *          the surface function
   */
  public Sphere(final SurfaceFunction theSurfaceFunction) {
    super(theSurfaceFunction, 1.0);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public Vector3D objectCoordinates(final Vector3D theIntersection) {
    final double u = Math.atan(Math.toRadians(theIntersection.getX() / theIntersection.getZ()));
    final double v = (theIntersection.getY() + 1.0) / 2.0;

    return new Vector3D(0.0, u, v);
  }

  @Override
  protected Intersection intersectAfterBoundingSphere(final Ray ray, final Intersection boundingSphereIntersection) {
    return boundingSphereIntersection;
  }
}
